package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "外卖分类功能")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @return Result<PageResult>
     * @throws
     * @description 分类的分页功能
     * @author Administrator
     * @param[1] categoryPageQueryDTO
     * @time 2023/11/28 0:00
     */
    @GetMapping("/page")
    @ApiOperation("分类的分页功能")
    public Result<PageResult> pageSelectCategory(CategoryPageQueryDTO categoryPageQueryDTO) {

        log.info("正在执行分类分页的功能.......分类信息为:{}", categoryPageQueryDTO);

        PageResult p = categoryService.pageSelectCategory(categoryPageQueryDTO);

        return Result.success(p);
    }

    /**
     * @return Result<List < Category>>
     * @throws
     * @description 根据类型查询分类
     * @author Administrator
     * @param[1] type
     * @time 2023/11/28 0:00
     */
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> ByTypeSelectCategory(Integer type) {

        log.info("正在执行根据类型查询分类的功能.......类型为:{}", type);

        List<Category> categories = categoryService.byTypeSelectCategory(type);

        return Result.success(categories);

    }

    /**
     * @return Result
     * @throws
     * @description 修改分类的功能
     * @author Administrator
     * @param[1] categoryDTO
     * @time 2023/11/27 23:59
     */
    @ApiOperation("修改分类的功能")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {

        log.info("正在执行修改分类的功能..........修改的分类信息为:{}", categoryDTO);

        categoryService.updateCategory(categoryDTO);


        return Result.success();
    }


    /**
     * @return Result
     * @throws
     * @description 新增分类的功能
     * @author Administrator
     * @param[1] categoryDTO
     * @time 2023/11/27 23:59
     */
    @ApiOperation("新增分类的功能")
    @PostMapping
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO) {

        log.info("正在执行新增分类的功能........新增的分类信息为:{}", categoryDTO);

        categoryService.insertCategory(categoryDTO);

        return Result.success();

    }

    /**
     * @return Result
     * @throws
     * @description 分类启用禁用功能
     * @author Administrator
     * @param[1] status
     * @param[2] id
     * @time 2023/11/27 23:59
     */
    @ApiOperation("分类启用禁用功能")
    @PostMapping("/status/{status}")
    public Result statusStartOrStop(@PathVariable Integer status, Long id) {


        log.info("分类启用禁用功能正在执行..........修改的功能为:{},执行的分类id为:{}", status, id);

        categoryService.statusStartOrStop(status, id);

        return Result.success();
    }


/**
 * @description 根据id删除分类的功能
 * @author Administrator
 * @param[1] id
 * @throws
 * @return Result
 * @time 2023/11/28 16:47
 */

    @ApiOperation("根据id删除分类功能")
    @DeleteMapping
    public Result deleteCategoryById(Long id) {

        log.info("正在执行根据id删除分类的功能.....id为:{}", id);

        categoryService.deleteCategoryById(id);

        return Result.success();
    }


}
